package com.kira.scaffoldmvc.Controller;

import cn.hutool.json.JSONUtil;
import com.kira.scaffoldmvc.POJO.*;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

@Slf4j
@RestController
@RequestMapping("/file")
public class FileController {


    @GetMapping("/getFile")
    public File file(HttpServletRequest httpServletRequest) throws IOException, InterruptedException {
        // 从 HttpServletRequest 中获取 Authorization 头信息
        String authorizationHeader = httpServletRequest.getHeader("Authorization");


        // 创建HttpClient实例
        HttpClient client = HttpClient.newHttpClient();

        // 创建HttpRequest实例，设置请求URL和请求方式
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create("http://47.119.148.251:7000/file/getFile"))
                .header("Authorization", authorizationHeader != null ? authorizationHeader : "")
                .GET()
                .build();

        // 发送请求并获取响应
        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
        log.info(response.body());
        // 处理响应
        System.out.println("响应状态码: " + response.statusCode());
        System.out.println("响应体内容: " + response.body());
        File file = JSONUtil.toBean(response.body(), File.class);

        return file;

    }

    @GetMapping("/getFileOverview")
    public GetFileOverview getFileOverview(HttpServletRequest httpServletRequest) throws IOException, InterruptedException {
        // 从 HttpServletRequest 中获取 Authorization 头信息
        String authorizationHeader = httpServletRequest.getHeader("Authorization");


        // 创建HttpClient实例
        HttpClient client = HttpClient.newHttpClient();

        // 创建HttpRequest实例，设置请求URL和请求方式
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create("http://47.119.148.251:7000/file/getFileOverview"))
                .header("Authorization", authorizationHeader != null ? authorizationHeader : "")
                .GET()
                .build();

        // 发送请求并获取响应
        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
        log.info(response.body());
        // 处理响应
        System.out.println("响应状态码: " + response.statusCode());
        System.out.println("响应体内容: " + response.body());
        GetFileOverview file = JSONUtil.toBean(response.body(), GetFileOverview.class);

        return file;

    }


    @PostMapping("/renameFile")
    public Response logout(@RequestBody FileInfo file, HttpServletRequest httpServletRequest) throws IOException, InterruptedException {
        // 从 HttpServletRequest 中获取 Authorization 头信息
        String authorizationHeader = httpServletRequest.getHeader("Authorization");

        // 创建HttpClient实例
        HttpClient client = HttpClient.newHttpClient();
        // 使用Hutool的JSONUtil将对象转换为JSON字符串
        String jsonBody = JSONUtil.toJsonStr(file);

        log.info("body里面的内容:{}",jsonBody);
        // 创建HttpRequest实例，设置请求URL和请求方式
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create("http://47.119.148.251:7000/file/renameFile"))
                .header("Authorization", authorizationHeader != null ? authorizationHeader : "")
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(jsonBody))
                .build();

        // 发送请求并获取响应
        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
        Response loginResponse = JSONUtil.toBean(response.body(), Response.class);
        log.info("内容:{}",loginResponse.toString());
        return loginResponse;

    }


    @PostMapping("/deleteFile")
    public ApiResponse delete( BucketFileId file, HttpServletRequest httpServletRequest) throws IOException, InterruptedException {
        // 从 HttpServletRequest 中获取 Authorization 头信息
        String authorizationHeader = httpServletRequest.getHeader("Authorization");

        // 创建HttpClient实例
        HttpClient client = HttpClient.newHttpClient();
        // 使用Hutool的JSONUtil将对象转换为JSON字符串
        String jsonBody = JSONUtil.toJsonStr(file);

        log.info("body里面的内容:{}",jsonBody);
        // 创建HttpRequest实例，设置请求URL和请求方式
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create("http://47.119.148.251:7000/file/deleteFile"))
                .header("Authorization", authorizationHeader != null ? authorizationHeader : "")
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(jsonBody))
                .build();

        // 发送请求并获取响应
        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
        ApiResponse loginResponse = JSONUtil.toBean(response.body(), ApiResponse.class);
        log.info("内容:{}",loginResponse.toString());
        return loginResponse;

    }


    @PostMapping("/shareFile")
    public ApiResponse delete(@RequestBody FileShareRequest file, HttpServletRequest httpServletRequest) throws IOException, InterruptedException {
        // 从 HttpServletRequest 中获取 Authorization 头信息
        String authorizationHeader = httpServletRequest.getHeader("Authorization");

        // 创建HttpClient实例
        HttpClient client = HttpClient.newHttpClient();
        // 使用Hutool的JSONUtil将对象转换为JSON字符串
        String jsonBody = JSONUtil.toJsonStr(file);
        log.info(jsonBody);
        log.info("body里面的内容:{}",jsonBody);
        // 创建HttpRequest实例，设置请求URL和请求方式
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create("http://47.119.148.251:7000/file/shareFile"))
                .header("Authorization", authorizationHeader != null ? authorizationHeader : "")
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(jsonBody))
                .build();

        // 发送请求并获取响应
        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
        ApiResponse loginResponse = JSONUtil.toBean(response.body(), ApiResponse.class);
        log.info("内容:{}",loginResponse.toString());
        return loginResponse;

    }
}


